<?php
class Advertisement_model extends Model{

	var $_datagrid_name = 'dt1';
	
    function Advertisement_model(){
        parent::Model();       
    }
	function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('advertisement');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,locationid,image,link,sortorder');    	
    	$this->db->order_by('locationid,sortorder,id');
    	$query = $this->db->get('advertisement');
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){
    		$dts[$i][1] = $this->_getlocationname($dts[$i][1]);
    		$dts[$i][2] = base_url().$this->config->item('advertisement_img_path').$dts[$i][2];
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
    function _getlocationname($id){
    	switch($id){
    		case 0: return lang('advertisement_location_left'); break;
    		case 1: return "Bên trái"; break;
			case 2: return 'Bên dưới'; break;
			case 3: return 'Chuyên trang địa ốc'; break;
    		default : return ''; break;
    	}
    }
    function getlocationlist(){
//    	$location[0]->id = 0;
//    	$location[0]->name = $this->_getlocationname(0);
    	$location[1]->id = 1;
    	$location[1]->name = $this->_getlocationname(1);
//		$location[2]->id = 2;
//    	$location[2]->name = $this->_getlocationname(2);
//		$location[3]->id = 3;
//    	$location[3]->name = $this->_getlocationname(3);
    	return $location;
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$this->_deleteimage($id);
    		$this->db->delete('advertisement',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			$this->_deleteimage($id[$i]);
	    		$this->db->delete('advertisement',array('id'=>$id[$i]));
    		}
    	}
    }
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('advertisement_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    		}    		    		
    		$this->db->insert('advertisement',array('locationid'=>$this->input->post('cbo_location'),'image'=>$image_name,'link'=>$this->input->post('txt_link'),'sortorder'=>0));
    		redirect('advertisement','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){    		
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('advertisement_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    			$this->_deleteimage($this->input->post('hidden_id'));
    		}
    		if(!empty($image_name)){
    			$this->db->set('image',$image_name);
    		}    		
    		$this->db->where('id',$this->input->post('hidden_id'));
    		$this->db->update('advertisement',array('locationid'=>$this->input->post('cbo_location'),'link'=>$this->input->post('txt_link')));
    		redirect('advertisement','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('advertisement',array('id'=>$id));
    	if($query->num_rows){
	    	return $query->row();
    	}
    	else return FALSE;
    }
    function save(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_4');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('advertisement',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}
    }
    function _deleteimage($id){
    	$res = $this->db->get_where('advertisement',array('id'=>$id));
    	$image = $res->row()->image;
    	if(file_exists($this->config->item('advertisement_img_path').$image)){
    		@unlink($this->config->item('advertisement_img_path').$image);
    	}
    }
}

/* End of file */